﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint;
using PhilWicklund.com.SiteCreationWorkflow.Utilities;
using Microsoft.SharePoint.Administration;
using PhilWicklund.com.SiteCreationWorkflow.Data;
using Microsoft.SharePoint.WorkflowActions;
using System.Text;

namespace PhilWicklund.com.SiteCreationWorkflow
{
	public sealed partial class SiteCreationWorkflow: SequentialWorkflowActivity
	{
		public SiteCreationWorkflow()
		{
			InitializeComponent();
		}

		public static DependencyProperty myTaskIDProperty = DependencyProperty.Register("myTaskID", typeof(System.Guid), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public Guid myTaskID
		{
			get
			{
				return ((System.Guid)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.myTaskIDProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.myTaskIDProperty, value);
			}
		}

		public static DependencyProperty myTaskPropertiesProperty = DependencyProperty.Register("myTaskProperties", typeof(Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties myTaskProperties
		{
			get
			{
				return ((Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.myTaskPropertiesProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.myTaskPropertiesProperty, value);
			}
		}

		private void IsJustificationPresent(object sender, ConditionalEventArgs e)
		{
			SPListItem item = onWorkflowActivated1.WorkflowProperties.Item;

			try
			{
				string purpose = item["Purpose for Site"].ToString();
				e.Result = false; // False: exit while loop, eg. while(0)
			}
			catch (NullReferenceException)
			{
				e.Result = true; // True: continue in while loop, eg. while(1)
			}
		}

		private void IsStatusNew(object sender, ConditionalEventArgs e)
		{
			SPListItem item = onWorkflowActivated1.WorkflowProperties.Item;
			ApprovalWorkflowStatus status = (ApprovalWorkflowStatus)Enum.Parse(
					typeof(ApprovalWorkflowStatus), item["Workflow State"].ToString()
				);

			// The site has either been approved or not approved, exit while loop
			if (status == ApprovalWorkflowStatus.Approved ||
				status == ApprovalWorkflowStatus.NotApproved)
			{
				// False: exit while loop, eg. while(0)
				e.Result = false;
			}
			else
			{
				// True: continue in while loop, eg. while(1)
				e.Result = true;
			}
		}

		public static DependencyProperty sendToSCAdmins_BodyProperty = DependencyProperty.Register("sendToSCAdmins_Body", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSCAdmins_Body
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_BodyProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_BodyProperty, value);
			}
		}

		public static DependencyProperty sendToSCAdmins_BCCProperty = DependencyProperty.Register("sendToSCAdmins_BCC", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSCAdmins_BCC
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_BCCProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_BCCProperty, value);
			}
		}

		public static DependencyProperty sendToSCAdmins_CCProperty = DependencyProperty.Register("sendToSCAdmins_CC", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSCAdmins_CC
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_CCProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_CCProperty, value);
			}
		}

		public static DependencyProperty sendToSCAdmins_FromProperty = DependencyProperty.Register("sendToSCAdmins_From", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSCAdmins_From
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_FromProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_FromProperty, value);
			}
		}

		public static DependencyProperty sendToSCAdmins_SubjectProperty = DependencyProperty.Register("sendToSCAdmins_Subject", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSCAdmins_Subject
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_SubjectProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_SubjectProperty, value);
			}
		}

		public static DependencyProperty sendToSCAdmins_ToProperty = DependencyProperty.Register("sendToSCAdmins_To", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSCAdmins_To
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_ToProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSCAdmins_ToProperty, value);
			}
		}

		private void BuildSCAdminEmail(object sender, EventArgs e)
		{
			WorkflowSettings settings = WorkflowSettings.GetSettings
				(onWorkflowActivated1.WorkflowProperties.Site.RootWeb.Url);

			sendToSCAdmins_BCC = "";
			sendToSCAdmins_Body = settings.scAdminEmail;
			sendToSCAdmins_CC = "";
			sendToSCAdmins_From = "";
			sendToSCAdmins_Subject = "New site approval request was submitted";

			
			StringBuilder to = new StringBuilder();
			foreach (SPUser scAdmin in onWorkflowActivated1.WorkflowProperties.Site.RootWeb.SiteAdministrators)
			{
				to.Append(scAdmin.Email);
				to.Append(";");
			}
			sendToSCAdmins_To = to.ToString();
		}

		public static DependencyProperty sendToSiteCreator_BCCProperty = DependencyProperty.Register("sendToSiteCreator_BCC", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSiteCreator_BCC
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_BCCProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_BCCProperty, value);
			}
		}

		public static DependencyProperty sendToSiteCreator_BodyProperty = DependencyProperty.Register("sendToSiteCreator_Body", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSiteCreator_Body
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_BodyProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_BodyProperty, value);
			}
		}

		public static DependencyProperty sendToSiteCreator_CCProperty = DependencyProperty.Register("sendToSiteCreator_CC", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSiteCreator_CC
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_CCProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_CCProperty, value);
			}
		}

		public static DependencyProperty sendToSiteCreator_SubjectProperty = DependencyProperty.Register("sendToSiteCreator_Subject", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSiteCreator_Subject
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_SubjectProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_SubjectProperty, value);
			}
		}

		public static DependencyProperty sendToSiteCreator_ToProperty = DependencyProperty.Register("sendToSiteCreator_To", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSiteCreator_To
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_ToProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_ToProperty, value);
			}
		}

		private void BuildSendToSiteCreator(object sender, EventArgs e)
		{
			WorkflowSettings settings = WorkflowSettings.GetSettings
				(onWorkflowActivated1.WorkflowProperties.Site.RootWeb.Url);

			sendToSiteCreator_BCC = "";
			sendToSiteCreator_Body = settings.siteCreatorEmail;
			sendToSiteCreator_CC = "";
			sendToSiteCreator_From = "";
			sendToSiteCreator_Subject = "Site approval status update";

			try
			{
				sendToSiteCreator_To = onWorkflowActivated1.WorkflowProperties.Item["Creator Email"].ToString();
			}
			catch (NullReferenceException)
			{
				//TODO - this user doesn't have an email address - skip/cancel activity?
				this.sendToSiteCreator.Enabled = false;
			}
		}

		private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
		{
			
		}

		private void onSiteJustificationAdded_Invoked(object sender, ExternalDataEventArgs e)
		{
			
		}

		private void logJustificationAdded_MethodInvoking(object sender, EventArgs e)
		{
			logJustificationAdded.HistoryOutcome = "Item Modified";
			logJustificationAdded.HistoryDescription = "Justification for the site has been added. The workflow is waiting for the workflow item to be approved or not approved.";
		}

		private void logWorkflowStarted_MethodInvoking(object sender, EventArgs e)
		{
			logWorkflowStarted.HistoryOutcome = "Workflow Started";
			logWorkflowStarted.HistoryDescription = "The Site approval workflow has been initiated. Workflow is pending, waiting for the site justification to be added.";
		}

		private void logWorkflowComplete_MethodInvoking(object sender, EventArgs e)
		{
			logWorkflowComplete.HistoryOutcome = "Workflow Complete";
			logWorkflowComplete.HistoryDescription = "The workflow is complete and the site was " + onWorkflowActivated1.WorkflowProperties.Item["Workflow State"].ToString();
		}

		private void IsItemApproved(object sender, ConditionalEventArgs e)
		{
			SPListItem item = onWorkflowActivated1.WorkflowProperties.Item;
			ApprovalWorkflowStatus status = (ApprovalWorkflowStatus)Enum.Parse(
					typeof(ApprovalWorkflowStatus), item["Workflow State"].ToString()
				);

			// The site has either been approved or not approved, exit while loop
			if (status == ApprovalWorkflowStatus.Approved)
			{
				e.Result = true;
			}
			else
			{
				// True: continue in while loop, eg. while(1)
				e.Result = false;
			}
		}

		private void ApproveWeb(object sender, EventArgs e)
		{
			string proposedSiteHyperlink = onWorkflowActivated1.WorkflowProperties.Item["Proposed Site"].ToString();
			string siteUrl = proposedSiteHyperlink.Substring(0, proposedSiteHyperlink.IndexOf(","));
			SPSite site = new SPSite(siteUrl);

			string relativeWebUrl = siteUrl.Substring(site.Url.Length + 1);
			SPWeb web = site.OpenWeb(relativeWebUrl);

			WorkFlowStatusService.SetWorkflowStatus(web.Url, ApprovalWorkflowStatus.Approved);

			web.Dispose();
			site.Dispose();
		}

		private void DeleteWeb(object sender, EventArgs e)
		{
			string proposedSiteHyperlink = onWorkflowActivated1.WorkflowProperties.Item["Proposed Site"].ToString();
			string siteUrl = proposedSiteHyperlink.Substring(0, proposedSiteHyperlink.IndexOf(","));

			SPSite site = new SPSite(siteUrl);

			string relativeWebUrl = siteUrl.Substring(site.Url.Length + 1);
			SPWeb web = site.OpenWeb(relativeWebUrl);

			web.Delete();

			web.Dispose();
			site.Dispose();
		}

		public static DependencyProperty sendToSiteCreator_FromProperty = DependencyProperty.Register("sendToSiteCreator_From", typeof(System.String), typeof(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow));

		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
		[BrowsableAttribute(true)]
		[CategoryAttribute("Misc")]
		public String sendToSiteCreator_From
		{
			get
			{
				return ((string)(base.GetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_FromProperty)));
			}
			set
			{
				base.SetValue(PhilWicklund.com.SiteCreationWorkflow.SiteCreationWorkflow.sendToSiteCreator_FromProperty, value);
			}
		}

		private void EmailsAreEnabled(object sender, ConditionalEventArgs e)
		{
			WorkflowSettings settings = WorkflowSettings.GetSettings
				(onWorkflowActivated1.WorkflowProperties.Site.RootWeb.Url);

			if (settings.enableEmails == true)
			{
				e.Result = true;
			}
			else
			{
				e.Result = false;
			}
		}
	}

}
